package com.kuaiying.kuai.ying.model.user;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

public class UserModel extends Model<UserModel>{

	private static final long serialVersionUID = -6984293973714614245L;
	
	public static final UserModel me = new UserModel();
	
	public static enum InfoType{ id, name, mobile, email};
	
	private static final String USER_CACHE = "user";
	
	public UserModel(){
		super( );
	}
	/**
	 * 所有 sql 与业务逻辑写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
	 */
	public Page<UserModel> paginate(int pageNumber, int pageSize) {
		return paginate(pageNumber, pageSize, "select *", "from  kuai_user order by id asc");
	}
	
//	/**
//	 * 根據用戶唯一字段判斷用戶是否存在，如存在則返回該用戶，否則返回null
//	 * @param userName：用戶名
//	 * @return：用戶信息記錄
//	 */
//	public UserModel isUserExistByInfo(String info, InfoType type ){
//		String sql = "select * from maiguo_user where ";
//		if( type.equals(InfoType.name ) ){
//			sql += "user_name = ?";
//		}else if( type.equals(InfoType.mobile ) ){
//			sql += "mobile = ?";
//		}else if( type.equals( InfoType.id ) ){
//			sql += "user_id = ?";
//		}else if( type.equals( InfoType.email ) ){
//			sql += "email = ?";
//		}
//		UserModel user = me.findFirst( sql, info );
//		return user;
//	}
	/**
	 * 根據用戶名和用戶密碼驗證用戶是否存在
	 * @param userName：用戶名
	 * @param pwd：用戶密碼
	 * @return：用戶信息或null
	 */
	public UserModel getUserByNameAndPwd(String userName, String pwd){
		String sql = "select * from kuai_user where user_name = '" + userName + "' and user_pwd ='" +  pwd + "'" ;
		UserModel user = me.findFirst(sql);
		return user;
	}


}


